﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web;

namespace UMC.Wiki.Activities
{
    class SubjectRowsActivity : WebActivity
    {

        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            //var subEntity = Utility.CMS.ObjectEntity<UMC.Data.Entities.Subject>();

            var webr = UMC.Web.WebResource.Instance();
            var form = request.SendValues ?? new UMC.Web.WebMeta(); 
            var search = new Subject();
            
            int limit = UMC.Data.Utility.IntParse(form["limit"] as string, 25);
            int start = UMC.Data.Utility.IntParse(form["start"] as string, 0);

            string sort = form[("sort")] as string;
            string dir = form[("dir")] as string;

            var category = form["Category"] as string;
            
            search.project_id = UMC.Data.Utility.UInt64Parse(category);

            var Keyword = (form["Keyword"] as string ?? String.Empty);
            switch (Keyword)
            {
                case "发布":
                case "等候发布":
                case "发布中":
                    search.Status = 0;
                    //subEntity.Where.And().Equal(new Data.Entities.Subject { Status = 0 });
                    Keyword = String.Empty;
                    break;
                case "已发布":
                    search.Status = 1;
                    //subEntity.Where.And().Equal(new Data.Entities.Subject { Status = 1 });
                    Keyword = String.Empty;
                    break;
                case "不发布":
                    search.Status = -1;
                    //subEntity.Where.And().Equal(new Data.Entities.Subject { Status = -1 });
                    Keyword = String.Empty;
                    break;
                default:
                    if (request.IsCashier == false)
                    {
                        search.Status = 1;
                    }
                    break;
            }
            if (String.IsNullOrEmpty(Keyword) == false)
            {
                search.Title = Keyword;
            }
            int nextIndex;
            var subs = DataFactory.Instance().Search(search, start, out nextIndex, limit);// new List<Subject>();
        
            var data = new System.Data.DataTable();
            data.Columns.Add("id");
            data.Columns.Add("src");
            data.Columns.Add("title");
            data.Columns.Add("desc");
            data.Columns.Add("time");
            data.Columns.Add("reply");
            data.Columns.Add("look");
            data.Columns.Add("last");
            data.Columns.Add("status");
            data.Columns.Add("poster");
            //data.Columns.Add("category");
            //var count = 108;
            foreach (var sub in subs)
            {
                if (sub.Status == -2 && request.IsCashier == false)
                {
                    continue;
                }
                var src = webr.ImageResolve( request.Url, sub.Id.Value, 1, "-3", UMC.Data.Utility.TimeSpan(sub.ReleaseTime.Value).ToString());


                //var cate = cates.Find(g => g.Id == sub.category_id);

                var s = "发布中";
                switch (sub.Status ?? 0)
                {
                    case -1:
                        s = "不发布";
                        break;
                    case 0:
                        s = "发布中";
                        break;
                    case 1:
                        s = "已发布";
                        break;
                }
                data.Rows.Add(sub.Id, src, sub.Title, sub.Description, UMC.Data.Utility.GetDate(sub.ReleaseTime)
                    , sub.Reply ?? 0, sub.Look ?? 0, UMC.Data.Utility.GetDate(sub.LastDate), s, sub.Poster
                   );
            }
            var hashc = new System.Collections.Hashtable();
            if (data.Rows.Count == 0 && start == 0)
            {
                hashc["msg"] = "现在还未有发布的新闻";
            }
            else
            {
                hashc["data"] = data;
                hashc["start"] = nextIndex;

            }
            response.Redirect(hashc);
        }

    }
}